#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#include "Log.h"
#include "LibManager.h"
#include "SDRS.h"
#include "OCR.h"
#include "Debug.h"
#include "Config.h"

#if 0

#include <cxcore.h>
#include <cv.h>
#include <highgui.h>

void judge(int code) {
    if (code == 0) {
        cout << "no subtitle detected" << "\n" << endl;
    } else if (code == 1) {
        cout << "\n" << "detected subtitle" << "\n" << endl;
    } else if (code == 2) {
        cout << "\n" << "!!! keyword detected !!!" << "\n" << endl;
    }
}

int main(int argc, char** argv) {
    const char *usage =
            "usage:sdrs -train|(-a filepath)|(-r filepath [...])|(-v filepath width height)\n\
	-train train classifier from current library\n\
	-a append library characters\n\
	-r recognise character embeded in image\n\
	-v recognise character embeded in video with width \"width\" and height \"height\"\n";

    const string configFilePath = "config/st.cfg";

    if (strcmp(argv[1], "-train") == 0) {
        if (argc < 2) {
            cout << usage;
            return 1;
        }

        Config::init(configFilePath);
        
        OCR::train();
    } else if (strcmp(argv[1], "-a") == 0) {
        if (argc < 3) {
            cout << usage;
            return 1;
        }

        Config::init(configFilePath);

        LibManager::appendChars(argv[2]);
    } else if (strcmp(argv[1], "-r") == 0) {
        if (argc < 3) {
            cout << usage;
            return 1;
        }

        SDRS::init(configFilePath);

        vector<ushort> res;
        uchar *imageData = NULL;
        int width;
        int height;

        for (int i = 2; i < argc; i++) {
            res.clear();

            imageData = Debug::loadImage(argv[i], width, height);
            if(imageData == NULL) {
                stringstream ss;
                ss << "can not find file \"" << argv[i] << "\"";
                Log::log(ss.str().c_str(), Log::ERROR);
            }

            int res = SDRS::detectKeyword(imageData, width, height);

            judge(res);

            delete []imageData;
        }

        SDRS::release();
    } else if (strcmp(argv[1], "-v") == 0) {
        if (argc < 5) {
            cout << usage;
            return 1;
        }

        int width = atoi(argv[3]);
        int height = atoi(argv[4]);

        SDRS::init(configFilePath);

        // the format of yuv file is frame by frame, and within each frame, grey image data lies first,
        // taking 2/3 length of frame, and then color message
        const int size = width*height*3/2;
        uchar *frame = new uchar[size];
        vector<ushort> res;

        ifstream fin(argv[2], ios::binary);
        if(!fin) {
            stringstream ss;
            ss << "can not find file \"" << argv[2] << "\"";
            Log::log(ss.str().c_str(), Log::ERROR);

            SDRS::release();
            return -1;
        }

        int frameCount = 0;
        int charCount = 0;

        Timer start;
        while(!fin.eof()) {
            fin.read((char *) frame, size);
            
            frameCount++;
            
            cout << "frame" << frameCount;
            cout << endl;

            int res = SDRS::detectKeyword(frame, width, height);
            judge(res);
        }
        fin.close();

        Timer end;
        long time = Timer::calcTimeUsed(start, end);
        cout << "characters per frame: " << charCount*1.0f/frameCount << endl;
        cout << "time cost per frame: " << time*1.0f/frameCount << endl;

        delete []frame;
        
        SDRS::release();
    }

    return EXIT_SUCCESS;
}

#endif

